package com.zzc.util.weixin;

import java.util.LinkedHashMap;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * @author fengyi on 2018-1-3.
 * 
 */
public class TemplateUtil {
	/**
	 * @param openid	用户ID
	 * @param templateId	模板ID
	 * @param url	底部链接
	 * @param remark	底部备注
	 * @param data	标准参数
	 * @param datas	自定义参数
	 * @return
	 */
	public static String toTemplateMsgText(String openid, String templateId,String url, String remark, Map<String,String> data, Map<String,String> datas) {

		Template template = new Template(openid,templateId);
		if (url != null && !"".equals(url))
			template.setUrl(url);
		StringBuffer sb = new StringBuffer("{");
		for (Map.Entry<String, String> entry : data.entrySet()) {
			sb.append(",\"" + entry.getKey() + "\":");
			sb.append("{\"value\":\"" + entry.getValue() + "\"}");	//,\"color\":\"#787878\"
		}
		
		StringBuffer sb2 = new StringBuffer();
		for (Map.Entry<String, String> entry : datas.entrySet()) {
			sb2.append("\\\\n" + entry.getKey() + ":" + entry.getValue());
		}
		sb.append(",\"remark\":{\"value\":\"REMARK\"}}");
		String json = sb.toString().replaceFirst("REMARK", sb2.toString().replace("remark:remark", "\\\\n" + remark)).replaceFirst(",", "");
		String jsonText = JSONObject.fromObject(template).toString();
		jsonText = jsonText.replace("\"DATA\"", json);
		
		System.out.println(json);
		System.out.println(jsonText);
		
		return jsonText;
	}
	public static void main(String[] args) {
	    Map<String, Object> map = new LinkedHashMap<String, Object>();
		Map<String,String> data = new LinkedHashMap<String, String>();
		Map<String,String> datas = new LinkedHashMap<String, String>();
		data.put("first", "尊敬的会员，您在本店成功消费!");
		data.put("keyword1", "1234567890");
		data.put("keyword2", "99.99");
		data.put("keyword3", "2018-01-01 12:29");
		data.put("keyword4", "东方希望天祥广场");
		
		datas.put("支付方式", "现金");
		datas.put("本次获取积分", "99.99");
		datas.put("积分余额", "199.99");
		datas.put("当前等级", "3");
		datas.put("remark", "remark");
		String remark ="更多详细信息，请前往微信会员中心查看！";
		String url = "http://weixin.zzcsoft6.com/ssmerp/weixin/vip/main?USERNAME=CDZFRJ";
		String jsonDate = TemplateUtil.toTemplateMsgText("odAyh0qbrvPceERGRVEC9DrTK2sg", "Gx2ddj2C27STLozlXg196I3qQKEcpMj7TRQhosLLiw0", url, remark, data, datas);
		
		map.put("openid", "odAyh0qbrvPceERGRVEC9DrTK2sg");
        map.put("USERNAME", "CDZZCRJ");
        map.put("remark", "12323");
        map.put("data", data);
        map.put("datas", datas);
		System.out.println(JSONObject.fromObject(map).toString());
		System.out.println(Weixin.sendTemplateMsg("wx9c3ee258f037761d", "acb07a8f0b484b2364ac7eb14668d0a6", jsonDate));
	}
	static class Template {
		private String touser;
		private String template_id;
		private String url = "";
		private String topcolor = "";
		private String data = "DATA";
		
		public Template(String touser, String template_id) {
			super();
			this.touser = touser;
			this.template_id = template_id;
		}
		public String getTouser() {
			return touser;
		}
		public void setTouser(String touser) {
			this.touser = touser;
		}
		public String getTemplate_id() {
			return template_id;
		}
		public void setTemplate_id(String template_id) {
			this.template_id = template_id;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getTopcolor() {
			return topcolor;
		}
		public void setTopcolor(String topcolor) {
			this.topcolor = topcolor;
		}
		public String getData() {
			return data;
		}
		public void setData(String data) {
			this.data = data;
		}
	}
}
